using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ComponentFactory.Krypton.Toolkit;
using System.Diagnostics;
using System.IO;

namespace Xtreme_Server
{
    /*public partial class MainWindow : ComponentFactory.Krypton.Toolkit.KryptonForm
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            btnWWWopen.Size = new Size((int)Math.Round((decimal)((btnStart.Width / 2) - 2)), 25);

            btnSomething.Location = new Point((int)Math.Round((decimal)((btnStart.Width / 2) + 5)), 33);
            btnSomething.Size = new Size((int)Math.Round((decimal)((btnStart.Width / 2) - 2)), 25);
        }
    }*/
    public partial class MainWindow : ComponentFactory.Krypton.Toolkit.KryptonForm
    {
        //public WebProgram.ws Program.ws = new WebProgram.ws();

        public MainWindow()
        {
            InitializeComponent();

            Program.ws.LogSent += this.LogReceivedThread;
            

            this.FormClosing += new FormClosingEventHandler(MainWindow_Closing);
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            btnWWWopen.Size = new Size((int)Math.Round((decimal)((btnStart.Width / 2) - 2)), 25);

            btnSomething.Location = new Point((int)Math.Round((decimal)((btnStart.Width / 2) + 5)), 33);
            btnSomething.Size = new Size((int)Math.Round((decimal)((btnStart.Width / 2) - 2)), 25);
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (Program.ws.Started)
            {
                btnStart.Enabled = false;
                btnStart.Text = "Stopping...";
                Program.ws.Stop();
                btnStart.Text = "Start";
                btnStart.Enabled = true;
            }
            else
            {
                btnStart.Enabled = false;
                btnStart.Text = "Starting...";
                Program.ws.Start();
                btnStart.Text = "Stop";
                btnStart.Enabled = true;
            }
        }
        string l;
        public void LogReceived(object sender, string log)
        {
            l = log;
            //StreamWriter wr = new StreamWriter(Path.Combine(Application.StartupPath, @"conf\tmp.dat"));
            //wr.Write(l);
            //wr.Close();

            //MessageBox.Show(l);
            //if (txLog.Text == "")
            //{
            // Thread.CurrentThread = new Thread(new ThreadStart(LogReceived2));
            //}
            txtLog.Text += l;
            prt.txtLog.Text += l;

            //txLog.Text += File.ReadAllText(Path.Combine(Application.StartupPath, @"conf\tmp.dat"));
            txtLog.Text += Environment.NewLine;
            prt.txtLog.Text += Environment.NewLine;
        }

        public void LogReceivedThread(object sender, string s)
        {
            Invoke(new WebServer.LogSentEventHandler(LogReceived), new object[] { this, s });
            abxt:
            try
            {
                prt.Invoke(new WebServer.LogSentEventHandler(LogReceived), new object[] { this, s });
            }
            catch (Exception)
            {
                Form x = (Form)prt;
                MainWindow y = (MainWindow)x;
                y.OnShown(new EventArgs());
                goto abxt;
            }
            
        }

        private void txLog_TextChanged(object sender, EventArgs e)
        {
            txtLog.SelectionStart = txtLog.Text.Length;
            txtLog.ScrollToCaret();
        }

        private void MainWindow_Closing(object sender, FormClosingEventArgs e)
        {
            Program.ws.Stop();
            Application.Exit();
        }

        private void btnWWWopen_Click(object sender, EventArgs e)
        {
            Process.Start("explorer.exe", Program.WWWRoot);
        }

        private void MainWindow_Load(object sender, EventArgs e)
        {
            if (prt == null) prt = new MainWindowEx();
        }

        private void btnMax_Click(object sender, EventArgs e)
        {
            #region OLD
            /*if (this.Size == new Size(199, 38))
            {
                while (this.Size != new Size(409, 295))
                {
                    if (this.Width != 409)
                    {
                        this.Width += 4;
                    }
                    if (this.Height != 295)
                    {
                        this.Height += 4;
                    }
                }
            }
            else
            {
                while (this.Size != new Size(199, 38))
                {
                    if (this.Width != 199)
                    {
                        if (this.Width >= 195)
                        {
                            this.Width -= 1;
                        }
                        else
                        {
                            this.Width -= 4;
                        }
                    }
                    if (this.Height != 38)
                    {
                        if (this.Height >= 38)
                        {
                            this.Height -= 1;
                        }
                        else
                        {
                            this.Height -= 4;
                        }
                    }
                }
            }*/
            #endregion

            if (this.Size == new Size(218, 39))
            {
                this.Size = new Size(409, 295);
                btnMax.Image = global::Xtreme_Server.Properties.Resources.arrow_in;
                btnSpecStart.Visible = false;
                this.Refresh();
            }
            else
            {
                this.Size = new Size(218, 39);
                btnMax.Image = global::Xtreme_Server.Properties.Resources.arrow_out;
                btnSpecStart.Visible = true;
                this.Refresh();
            }
        }

        private void btnSomething_Click(object sender, EventArgs e)
        {
            
            prt.Location = this.Location;
            prt.txtLog.Text = this.txtLog.Text;
            prt.prt = this;
            this.Hide();
            prt.Show();
            this.ShowInTaskbar = false;
            this.Visible = false;
        }

        public MainWindowEx prt { get; set; }
    }
}